package cn.steel.controller;

import cn.steel.domain.entity.User;
import cn.steel.service.RegisterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

@Controller
public class RegisterController {

    private static final Logger logger = LoggerFactory.getLogger(RegisterController.class);

    @Autowired
    private RegisterService  registerService;

    // 显示注册页面
    @GetMapping("/register")
    public String showRegisterForm(Model model,
                                   @RequestParam(value = "l", required = false) String lang) {
        model.addAttribute("user", new User());
        model.addAttribute("roles", getRoleList());
        model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
        return "register";
    }

    // 处理注册提交
    @PostMapping("/register")
    public String doRegister(@ModelAttribute User user,
                             @RequestParam String confirmPassword,
                             Model model) {
        logger.info("用户注册请求: {}", user.getUsername());

        // 校验密码一致性
        if (!user.getPassword().equals(confirmPassword)) {
            model.addAttribute("error", "两次输入的密码不一致");
            model.addAttribute("roles", getRoleList());
            model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
            return "register";
        }

        // 校验用户名是否存在
        if (registerService.existsByUsername(user.getUsername())) {
            model.addAttribute("error", "用户名已存在");
            model.addAttribute("roles", getRoleList());
            model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
            return "register";
        }

        if ("SUPPLIER".equals(user.getRole())) {
            if (user.getCompany() == null || user.getCompany().trim().isEmpty()) {
                model.addAttribute("error", "供应商必须提供公司名称");
                model.addAttribute("roles", getRoleList());
                model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
                return "register";
            }
            if (user.getPhone() == null || user.getPhone().trim().isEmpty()) {
                model.addAttribute("error", "供应商必须提供联系电话");
                model.addAttribute("roles", getRoleList());
                model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
                return "register";
            }
            if (user.getAddress() == null || user.getAddress().trim().isEmpty()) {
                model.addAttribute("error", "供应商必须提供地址");
                model.addAttribute("roles", getRoleList());
                model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
                return "register";
            }
        }

        // 保存用户
        registerService.register(user);
        logger.info("用户注册成功: {}", user.getUsername());

        return "redirect:/login";
    }

    // 角色列表（String 类型）
    private List<String> getRoleList() {
        return Arrays.asList("SUPPLIER", "BUYER");
    }
}
